package edu.unlv.cs.ladders.entities;

import edu.unlv.cs.ladders.entities.Tournament;
import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect TournamentDataOnDemand_Roo_DataOnDemand {
    
    declare @type: TournamentDataOnDemand: @Component;
    
    private Random TournamentDataOnDemand.rnd = new SecureRandom();
    
    private List<Tournament> TournamentDataOnDemand.data;
    
    public Tournament TournamentDataOnDemand.getNewTransientTournament(int index) {
        edu.unlv.cs.ladders.entities.Tournament obj = new edu.unlv.cs.ladders.entities.Tournament();
        obj.setClosed(new Boolean(true));
        obj.setEndDate(null);
        obj.setNote("note_" + index);
        obj.setStartDate(null);
        return obj;
    }
    
    public Tournament TournamentDataOnDemand.getSpecificTournament(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Tournament obj = data.get(index);
        return Tournament.findTournament(obj.getId());
    }
    
    public Tournament TournamentDataOnDemand.getRandomTournament() {
        init();
        Tournament obj = data.get(rnd.nextInt(data.size()));
        return Tournament.findTournament(obj.getId());
    }
    
    public boolean TournamentDataOnDemand.modifyTournament(Tournament obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void TournamentDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = edu.unlv.cs.ladders.entities.Tournament.findTournamentEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Tournament' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<edu.unlv.cs.ladders.entities.Tournament>();
        for (int i = 0; i < 10; i++) {
            edu.unlv.cs.ladders.entities.Tournament obj = getNewTransientTournament(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
